<?php
namespace admin\models;

use common\models\SearchInterface;
use common\models\SearchTrait;

class BookSearch extends Book implements SearchInterface
{
    use SearchTrait;

    public $keyword;

    public function rules()
    {
        return [
            [['cat'], 'integer'],
            [['keyword', 'name'], 'trim'],
        ];
    }

    public function prepareQuery($query)
    {
        // 关键词
        if ($this->keyword) {
            $query->andFilterWhere([
                'OR',
                ['like', 'name', $this->keyword],
                ['like', 'author', $this->keyword],
            ]);
        }

        // 筛选
        $query->andFilterWhere(['cat' => $this->cat])
            ->orderBy(['id' => SORT_DESC]);
    }

}
